const express = require('express')
const expressStatic = require('express-static')
const cookieParser = require('cookie-parser')
const cookieSession = require('cookie-session')
const bodyParser = require('body-parser')
const consolidate = require('consolidate')
const ejs = require('ejs')
const multer = require('multer')
const mysql = require('mysql')
const db = mysql.createPool({
		host: 'localhost',
		user: 'root',
		password:'root',
		database: 'blog'
	})
let server = express()
let objMulter = multer({dest: './www/upload/'})
server.listen(8080)

//解析cookie
server.use(cookieParser('asdfasdfasdfasdf'))
//使用session
let arr = []
for(i = 0; i < 10000; i++){
	arr.push(Math.random())
}
server.use(cookieSession({name: 'sess_id',keys: arr,maxAge: 20 * 3600 * 1000}))

//post数据
server.use(bodyParser.urlencoded({extended: false}))
server.use(objMulter.any())
//配置模板引擎
//输出什么东西
server.set('view engine', 'html')
server.set('views', './template')
//那种模板引擎
server.engine('html', consolidate.ejs)
//接收用户请求
server.get('/', (req,res,next) => {
	
	db.query('SELECT * FROM banner_table',(err,data) => {
		if(err){
			res.status(500).send('database err').end()
		}else{
			
			res.banners = data;
			next();
			// res.render('index.ejs', {banners: data})
		}
	})
})
server.get('/', (req,res,next) => {
	
	db.query('SELECT ID,title,summary FROM article_table',(err,data) => {
		if(err){
			res.status(500).send('database err').end()
		}else{
			res.articles = data;
			next()
		}
	})
})
server.get('/', (req,res) => {
	res.render('index.ejs', {banners: res.banners,articles: res.articles})
})
server.get('/articles', (req,res) => {
	res.render('conText.ejs', {})
})





//static数据
server.use(expressStatic('./www'))